From: Jeroen van der Heijden Date: Wed, 19 Jun 2019 14:59:41 +0000 (+0200) Subject: Fixed health check X-Git-Tag: archive/raspbian/2.0.44-1+rpi1~1^2~3^2~6^2~6 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=f8df41cc0438e98ba64c7e1f7a563419b390ae58;p=siridb-server.git Fixed health check --- diff --git a/itest/testing/server.py b/itest/testing/server.py index 81186887..26f5a98a 100644 --- a/itest/testing/server.py +++ b/itest/testing/server.py @@ -52,6 +52,7 @@ class Server: self.pipe_name = \ 'siridb_client.sock' if not self.enable_pipe_support else \ pipe_name + self.http_status_port = 8080 + n self.listen_client_port = 9000 + n self.listen_backend_port = 9010 + n self.buffer_sync_interval = buffer_sync_interval @@ -95,6 +96,7 @@ class Server: config.set('siridb', 'enable_shard_compression', int(self.compression)) config.set('siridb', 'enable_pipe_support', self.enable_pipe_support) config.set('siridb', 'pipe_client_name', self.pipe_name) + config.set('siridb', 'http_status_port', self.http_status_port) with open(self.cfgfile, 'w') as configfile: config.write(configfile) diff --git a/siridb.conf b/siridb.conf index 2a25d70b..00eef951 100644 --- a/siridb.conf +++ b/siridb.conf @@ -90,4 +90,4 @@ pipe_client_name = siridb_client.sock # which can be used for readyness and liveness requests. # #http_status_port = 8080 - +http_status_port = 0 diff --git a/src/siri/cfg/cfg.c b/src/siri/cfg/cfg.c index 0c3e4fed..1de320fc 100644 --- a/src/siri/cfg/cfg.c +++ b/src/siri/cfg/cfg.c @@ -113,6 +113,15 @@ void siri_cfg_init(siri_t * siri) &tmp); siri_cfg.heartbeat_interval = (uint16_t) tmp; + tmp = siri_cfg.http_status_port; + SIRI_CFG_read_uint( + cfgparser, + "http_status_port", + 0, + 65535, + &tmp); + siri_cfg.http_status_port = (uint16_t) tmp; + SIRI_CFG_read_default_db_path(cfgparser); SIRI_CFG_read_max_open_files(cfgparser); SIRI_CFG_read_ip_support(cfgparser); diff --git a/src/siri/health.c b/src/siri/health.c index 53a7bd46..79d6e152 100644 --- a/src/siri/health.c +++ b/src/siri/health.c @@ -40,31 +40,31 @@ "\r\n" \ "REINDEXING\n" -#define READY_RESPONSE \ +#define BMODE_RESPONSE \ "HTTP/1.1 200 OK\r\n" \ "Content-Type: text/plain\r\n" \ - "Content-Length: 6\r\n" \ + "Content-Length: 12\r\n" \ "\r\n" \ - "READY\n" + "BACKUP MODE\n" /* static response buffers */ -static uv_buf_t web__uv_ok_buf; -static uv_buf_t web__uv_nok_buf; -static uv_buf_t web__uv_nfound_buf; -static uv_buf_t web__uv_sync_buf; -static uv_buf_t web__uv_reidx_buf; -static uv_buf_t web__uv_ready_buf; +static uv_buf_t health__uv_ok_buf; +static uv_buf_t health__uv_nok_buf; +static uv_buf_t health__uv_nfound_buf; +static uv_buf_t health__uv_sync_buf; +static uv_buf_t health__uv_reidx_buf; +static uv_buf_t health__uv_bmode_buf; -static uv_tcp_t web__uv_server; -static http_parser_settings web__settings; +static uv_tcp_t health__uv_server; +static http_parser_settings health__settings; -static void web__close_cb(uv_handle_t * handle) +static void health__close_cb(uv_handle_t * handle) { siri_health_request_t * web_request = handle->data; free(web_request); } -static void web__alloc_cb( +static void health__alloc_cb( uv_handle_t * handle __attribute__((unused)), size_t sugsz __attribute__((unused)), uv_buf_t * buf) @@ -73,7 +73,7 @@ static void web__alloc_cb( buf->len = buf->base ? HTTP_MAX_HEADER_SIZE : 0; } -static void web__data_cb( +static void health__data_cb( uv_stream_t * uvstream, ssize_t n, const uv_buf_t * buf) @@ -98,7 +98,7 @@ static void web__data_cb( parsed = http_parser_execute( &web_request->parser, - &web__settings, + &health__settings, buf->base, n); if (web_request->parser.upgrade) @@ -116,10 +116,10 @@ done: free(buf->base); } -static uv_buf_t * web__get_status_response(void) +static uv_buf_t * health__get_status_response(void) { siridb_t * siridb; - uint8_t flags = 0; + uint8_t flags = SERVER_FLAG_RUNNING; llist_node_t * siridb_node; siridb_node = siri.siridb_list->first; @@ -132,19 +132,25 @@ static uv_buf_t * web__get_status_response(void) if (flags & SERVER_FLAG_SYNCHRONIZING) { - return &web__uv_sync_buf; + return &health__uv_sync_buf; } if (flags & SERVER_FLAG_REINDEXING) { - return &web__uv_sync_buf; + return &health__uv_reidx_buf; } - return &web__uv_nok_buf; + if (flags & SERVER_FLAG_BACKUP_MODE) + { + return &health__uv_bmode_buf; + } + return flags == SERVER_FLAG_RUNNING + ? &health__uv_ok_buf + : &health__uv_nok_buf; } -static uv_buf_t * web__get_ready_response(void) +static uv_buf_t * health__get_ready_response(void) { siridb_t * siridb; - uint8_t flags = 0; + uint8_t flags = SERVER_FLAG_RUNNING; llist_node_t * siridb_node; siridb_node = siri.siridb_list->first; @@ -155,10 +161,12 @@ static uv_buf_t * web__get_ready_response(void) siridb_node = siridb_node->next; } - return flags == SERVER_FLAG_RUNNING ? &web__uv_ok_buf : &web__uv_nok_buf; + return flags == SERVER_FLAG_RUNNING + ? &health__uv_ok_buf + : &health__uv_nok_buf; } -static int web__url_cb(http_parser * parser, const char * at, size_t length) +static int health__url_cb(http_parser * parser, const char * at, size_t length) { siri_health_request_t * web_request = parser->data; @@ -167,23 +175,23 @@ static int web__url_cb(http_parser * parser, const char * at, size_t length) /* status response */ = ((length == 1 && *at == '/') || (length == 7 && memcmp(at, "/status", 7) == 0)) - ? web__get_status_response() + ? health__get_status_response() /* ready response */ : (length == 6 && memcmp(at, "/ready", 6) == 0) - ? web__get_ready_response() + ? health__get_ready_response() /* healthy response */ : (length == 8 && memcmp(at, "/healthy", 8) == 0) - ? &web__uv_ok_buf + ? &health__uv_ok_buf /* everything else */ - : &web__uv_nfound_buf; + : &health__uv_nfound_buf; return 0; } -static void web__write_cb(uv_write_t * req, int status) +static void health__write_cb(uv_write_t * req, int status) { if (status) log_error("error writing HTTP response: `%s`", uv_strerror(status)); @@ -191,23 +199,23 @@ static void web__write_cb(uv_write_t * req, int status) siri_health_close((siri_health_request_t *) req->handle->data); } -static int web__message_complete_cb(http_parser * parser) +static int health__message_complete_cb(http_parser * parser) { siri_health_request_t * web_request = parser->data; if (!web_request->response) - web_request->response = &web__uv_nfound_buf; + web_request->response = &health__uv_nfound_buf; (void) uv_write( &web_request->req, &web_request->uvstream, web_request->response, 1, - web__write_cb); + health__write_cb); return 0; } -static void web__connection_cb(uv_stream_t * server, int status) +static void health__connection_cb(uv_stream_t * server, int status) { int rc; siri_health_request_t * web_request; @@ -244,7 +252,7 @@ static void web__connection_cb(uv_stream_t * server, int status) http_parser_init(&web_request->parser, HTTP_REQUEST); - rc = uv_read_start(&web_request->uvstream, web__alloc_cb, web__data_cb); + rc = uv_read_start(&web_request->uvstream, health__alloc_cb, health__data_cb); if (rc) { log_error("cannot read HTTP request: `%s`", uv_strerror(rc)); @@ -261,32 +269,32 @@ int siri_health_init(void) (void) uv_ip6_addr("::", (int) port, (struct sockaddr_in6 *) &addr); - web__uv_ok_buf = + health__uv_ok_buf = uv_buf_init(OK_RESPONSE, strlen(OK_RESPONSE)); - web__uv_nok_buf = + health__uv_nok_buf = uv_buf_init(NOK_RESPONSE, strlen(NOK_RESPONSE)); - web__uv_nfound_buf = + health__uv_nfound_buf = uv_buf_init(NFOUND_RESPONSE, strlen(NFOUND_RESPONSE)); - web__uv_sync_buf = + health__uv_sync_buf = uv_buf_init(SYNC_RESPONSE, strlen(SYNC_RESPONSE)); - web__uv_reidx_buf = + health__uv_reidx_buf = uv_buf_init(REIDX_RESPONSE, strlen(REIDX_RESPONSE)); - web__uv_ready_buf = - uv_buf_init(READY_RESPONSE, strlen(READY_RESPONSE)); + health__uv_bmode_buf = + uv_buf_init(BMODE_RESPONSE, strlen(BMODE_RESPONSE)); - web__settings.on_url = web__url_cb; - web__settings.on_message_complete = web__message_complete_cb; + health__settings.on_url = health__url_cb; + health__settings.on_message_complete = health__message_complete_cb; if ( - (rc = uv_tcp_init(siri.loop, &web__uv_server)) || + (rc = uv_tcp_init(siri.loop, &health__uv_server)) || (rc = uv_tcp_bind( - &web__uv_server, + &health__uv_server, (const struct sockaddr *) &addr, 0)) || (rc = uv_listen( - (uv_stream_t *) &web__uv_server, + (uv_stream_t *) &health__uv_server, 128, - web__connection_cb))) + health__connection_cb))) { log_error("error initializing HTTP status server on port %u: `%s`", port, @@ -294,7 +302,7 @@ int siri_health_init(void) return -1; } - log_info("start listening for HTTP status requests on TCP port %u", port); + log_info("Start listening for HTTP status requests on TCP port %u", port); return 0; } @@ -303,5 +311,5 @@ void siri_health_close(siri_health_request_t * web_request) if (!web_request || web_request->is_closed) return; web_request->is_closed = true; - uv_close((uv_handle_t *) &web_request->uvstream, web__close_cb); + uv_close((uv_handle_t *) &web_request->uvstream, health__close_cb); }